package demo1;

public class MakeArray {
    //定义成员变量
    private int line;

    //无参构造方法
    public MakeArray() {
    }

    //有参构造方法
    public MakeArray(int line) {
        setLine(line);
    }

    public int getLine() {
        return line;
    }

    public void setLine(int line) {
        this.line = line;
    }

    /**
     * 初始化二维数组
     * @return arr
     */
    public int[][] initArray(){
        int row = getLine(); //行
        int column = getLine(); //列
        int nums = 1; //默认值1

        //定义二维数组的行和列
        int[][] arr = new int[column][row];
        //填充内容
        for (int i=0; i<arr.length; i++){
            for(int j=0; j<arr[i].length; j++){
                arr[i][j] = nums;
            }
        }
        return arr;
    }

    /**
     * 打印二维数组
     */
    public void showArray(int[][] twoarray){
        for(int i=0; i<twoarray.length; i++){
            for(int j=0; j<twoarray[i].length; j++){
                System.out.print(twoarray[i][j]);
            }
            System.out.println(" ");
        }
    }

    /**
     * 获取所有行和列中元素累加和
     */
    public void getAllSum(int[][] twoarray){
        for(int i=0; i<twoarray.length; i++){
            int rowSum = 0;
            int colSum = 0;
            for(int j=0; j<twoarray[i].length; j++){
                //行累加
                rowSum += twoarray[i][j];
                colSum += twoarray[j][i];
            }
            System.out.println("第"+(i+1)+"行的和："+rowSum);
            System.out.println("第"+(i+1)+"列的和："+colSum);
            System.out.println("-------------------");
        }
    }

    /**
     * 获取中左上角到右下角和右上角到左下角所有元素的累加和
     */
    public void getLRTDSum(int[][] twoarray){
        int topLeftSum = 0;
        int topRightSum = 0;
        for(int i=0; i<twoarray.length; i++){
            //左上角到右下角元素累加
            topLeftSum += twoarray[i][i];
            for(int j=0; j<twoarray[i].length; j++){
                if(twoarray.length == i + j + 1 ) {
                    //右上角到左下角元素累加
                    topRightSum += twoarray[i][j];
                }
            }
        }
        System.out.println("二维数组中，左上角到右下角所有元素的和是：" + topLeftSum);
        System.out.println("二维数组中，右上角到左下角所有元素的和是：" + topRightSum);
    }
}
